package com.woniuxy.learnmap.controller;

import com.aliyun.oss.OSSClient;
import com.woniuxy.commons.dto.*;
import com.woniuxy.commons.entity.*;
import com.woniuxy.commons.exception.EtmsServiceException;
import com.woniuxy.commons.exception.learnmap.*;
import com.woniuxy.commons.service.RemoteCourseService;
import com.woniuxy.commons.service.RemoteExamService;
import com.woniuxy.learnmap.configuration.AliYunOSSConfiguration;
import com.woniuxy.learnmap.service.CertificateService;
import com.woniuxy.learnmap.service.LearnMapService;
import com.woniuxy.learnmap.service.RouteService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.util.List;
import java.util.UUID;

/**
 * @Auther: 王敞
 * @E-mail： 365799712@qq.com
 * @Date: 2020/07/20/10:29
 * @Description:
 */
@RestController

public class LearnMapController {
    @Autowired
    private RouteService routeService;
    @Autowired
    private LearnMapService learnMapService;
    @Autowired
    private CertificateService certificateService;
    @Autowired
    private RemoteCourseService remoteCourseService;
    @Autowired
    private RemoteExamService remoteExamService;

    /*
     *
     * @Description: 新增一条路径
     * @Param: [routeDto]
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.lang.Void>
     * @Author: 王敞
     * @Date: 16:20 2020/7/22
     **/
    @PostMapping("/addRoute")
    public ResponseEntity<Void> addRoute(@RequestBody RouteDto routeDto) {
        Route route = new Route();
        route.setCertificateid(routeDto.getCertificateid()).setDescription(routeDto.getDescription())
                .setName(routeDto.getName()).setStyle(routeDto.getStyle());
        try {
            routeService.addRoute(route, routeDto.getRouteDetails());
            return ResponseEntity.SUCCESS_RESPONSE;
        } catch (EtmsServiceException e) {
            System.out.println(e);
            if (e.getCause() instanceof RouteNameIsAlReadyExistException) {
                return new ResponseEntity<>(114, "路径名已存在");
            } else if (e.getCause() instanceof RouteDetailALReadyExistException) {
                return new ResponseEntity<>(121, "该种路径详情组合已存在");
            } else {
                return new ResponseEntity<>(310, "系统维护中");
            }
        }


    }

    /*
     *
     * @Description: 新增一个学习地图
     * @Param: [learnMapDto]
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.lang.Void>
     * @Author: 王敞
     * @Date: 16:20 2020/7/22
     **/
    @PostMapping("/addLearnMap")
    public ResponseEntity<Void> addLearnMap(@RequestBody LearnMapDto learnMapDto) {
        LearnMap learnMap = new LearnMap();
        learnMap.setDescription(learnMapDto.getDescription())
                .setName(learnMapDto.getName()).setStatus(learnMapDto.getStatus())
                .setImage(learnMapDto.getImage());
        try {
            learnMapService.addLearnMap(learnMap, learnMapDto.getLearnMapDetailDtos());
            return ResponseEntity.SUCCESS_RESPONSE;
        } catch (EtmsServiceException e) {
            if (e.getCause() instanceof LearnMapNameAlReadyExistException) {
                return new ResponseEntity<Void>().setCode(233).setMessage("学习地图名已存在");
            } else if (e.getCause() instanceof LearnMapDetailRecordAlReadyExistException) {
                return new ResponseEntity<Void>().setCode(257).setMessage("该学习地图详情已经使用过了");
            } else {
                return new ResponseEntity<Void>().setCode(433).setMessage("系统维护中");
            }
        }
    }

    /*
     *
     * @Description: 增加学习地图封面
     * @Param: [file]
     * @return: com.woniuxy.commons.entity.ResponseEntity<java.lang.Void>
     * @Author: 王敞
     * @Date: 16:59 2020/7/22
     **/
    @RequestMapping("/addImage")
    public ResponseEntity<String> addImage(MultipartFile file) {
        try {
            String url = learnMapService.uploadImage(file);
            return new ResponseEntity<>(url);
        } catch (EtmsServiceException e) {
            if (e.getCause() instanceof NoImageException) {
                return new ResponseEntity<String>().setCode(330).setMessage("请注意上传必须为图片格式");
            }
            return new ResponseEntity<String>().setCode(440).setMessage("系统维护中");
        }

    }

    @PostMapping("/addCertificate")
    public ResponseEntity<Void> addCertificate(@RequestBody Certificate certificate) {
        try {
            certificateService.addCertificate(certificate);
            return ResponseEntity.SUCCESS_RESPONSE;
        } catch (EtmsServiceException e) {
            e.printStackTrace();
            if (e.getCause() instanceof CertificateNameAlReadyExistException) {
                return new ResponseEntity<Void>().setCode(346).setMessage("证书名字已被使用");
            } else {
                return new ResponseEntity<Void>().setCode(330).setMessage("系统维护中");
            }
        }
    }

    @GetMapping("/findAllCertificates")
    public ResponseEntity<List<Certificate>> findAllCertificates() {
        List<Certificate> certificates = certificateService.findAllCertificates();
        return new ResponseEntity<>(certificates);
    }

    @PostMapping("/findCourses")
    public ResponseEntity<PageBean<CourseDto>> findCoursesByCondition(@RequestBody CourseDto courseDto) {
        return remoteCourseService.queryCoursesByCondition(courseDto);
    }

    @PostMapping("/findLearnMapsByPage")
    public ResponseEntity<PageBean<LearnMapDto>> findLearnMapsByPage(@RequestBody LearnMapDto learnMapDto) {
        try {
            PageBean<LearnMapDto> pb = learnMapService.findLearnMapsByPage(learnMapDto.getPage(), learnMapDto.getPageSize());
            return new ResponseEntity<PageBean<LearnMapDto>>(pb);
        } catch (EtmsServiceException e) {
            return new ResponseEntity<PageBean<LearnMapDto>>().setCode(330).setMessage("系统维护中");
        }
    }

    @PostMapping("/findRoutes")
    public ResponseEntity<PageBean<RouteDto>> findRoutesByCondition(@RequestBody RouteSearchDto routeSearchDto) {
        try {
            PageBean<RouteDto> pb = routeService.findRoutesByPage(routeSearchDto);
            return new ResponseEntity<>(pb);
        } catch (EtmsServiceException e) {
            return new ResponseEntity<PageBean<RouteDto>>().setCode(330).setMessage("系统维护中");
        }
    }

    @PostMapping("/findLearnMapDetails")
    public ResponseEntity<List<LearnMapDetailDto>> findLearnMapDetailsByLmid(@RequestBody LearnMap learnMap) {
        try {
            List<LearnMapDetailDto> learnMapDetailDtos = learnMapService.findLearnMapDetailByLMId(learnMap.getId());
            return new ResponseEntity<>(learnMapDetailDtos);
        } catch (EtmsServiceException e) {
            return new ResponseEntity<List<LearnMapDetailDto>>().setCode(330).setMessage("系统维护中");
        }
    }

    @PostMapping("/delRoute")
    public ResponseEntity<Void> delRouteById(@RequestBody Route route) {
        try {
            routeService.deleteRouteById(route.getRouteid());
            return ResponseEntity.SUCCESS_RESPONSE;
        } catch (EtmsServiceException e) {
            return new ResponseEntity<Void>().setMessage("系统维护中").setCode(330);
        }
    }

    @PostMapping("/findRouteDetails")
    public ResponseEntity<List<RouteDetailDto>> findRouteDetailsByRouteId(@RequestBody Route route) {
        try {
            List<RouteDetailDto> data = routeService.findRouteDetails(route.getRouteid());
            return new ResponseEntity<>(data);
        } catch (EtmsServiceException e) {
            e.printStackTrace();
            return new ResponseEntity<List<RouteDetailDto>>().setCode(330).setMessage("系统维护中");
        }
    }

    @RequestMapping("/findExams")
    public ResponseEntity findExams(@RequestBody ExamDto examDto) {
        return remoteExamService.conditionAndPageSearch(examDto);
    }
    @RequestMapping("/del")
    public ResponseEntity<Void> delLearnMap(@RequestBody LearnMap learnMap){
        try {
            learnMapService.del(learnMap.getId());
            return ResponseEntity.SUCCESS_RESPONSE;
        }catch (EtmsServiceException e){
            return new ResponseEntity<Void>().setCode(330).setMessage("系统维护中");
        }
    }
}
